package com.mkvld.appietijdenv2.data;

import java.io.Serializable;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import com.mkvld.appietijdenv2.data.Shift.Allowance;

public class WeekShifts implements Iterable<DayShifts>, Serializable {
	private static final long serialVersionUID = -391936830122853379L;
	private DayShifts[] shifts;
	private double workTime, sickHours;
	private Allowance allowance;
	
	public WeekShifts() {
		shifts = new DayShifts[7];
		for (int i = 0; i < shifts.length; i++)
			shifts[i] = new DayShifts();
	}
	
	public WeekShifts(int contract, List<Shift> shifts) {
		this.shifts = new DayShifts[7];
		for (int i = 0; i < this.shifts.length; i++)
			this.shifts[i] = new DayShifts();
		for (Shift shift : shifts) {
			add(shift);
		}
		calculateTimes(contract);
	}
	
	public void calculateTimes(int contract) {
		workTime = 0;
		sickHours = 0;
		allowance = new Allowance();
		
		for (DayShifts dayShifts : shifts) {
			dayShifts.calculateTimes();
			
			workTime += dayShifts.getWorkTime();
			sickHours += dayShifts.getSickHours();
			allowance.add(dayShifts.getAllowance());
		}
		
		if (sickHours > contract)
			sickHours = contract;
		
		if (workTime <= 12)
			allowance.dayAllowance = 0;
	}
	
	public DayShifts[] getShifts() {
		return shifts;
	}

	public void add(Shift shift) {
		shifts[shift.getDay()].add(shift);
	}
	
	public double getWorkTime() {
		return workTime;
	}
	
	public double getSickHours() {
		return sickHours;
	}
	
	public Allowance getDayAllowance(int day) {
		Allowance allowance = shifts[day].getAllowance();
		if (getWorkTime() <= 12)
			allowance.dayAllowance = 0;
		return allowance;
	}

	public Allowance getAllowance() {
		return allowance;
	}

	@Override
	public Iterator<DayShifts> iterator() {
		return Arrays.asList(shifts).iterator();
	}
}
